//
//  NetworkService.swift
//  jmwebseoApp1803
//
//  Created by YQ05 on 2018/3/30.
//  Copyright © 2018年 YQ05. All rights reserved.
//

import Foundation
import Moya

enum NetworkService{
    case category
    case showCateNewsList(id: Int)
    case submitComment(postId: Int, name: String, email: String, content: String)
}

extension NetworkService: TargetType {
    var baseURL: URL { return URL(string: "https://www.jmwebseo.cn/api")! }
    
    var path: String {
        switch self {
        case .category:
            return "/get_category_index"
            
        case .showCateNewsList:
            return "/get_category_posts/"
        case .submitComment:
            return "/respond/submit_comment/"
          
        }
    
    }
    
    var method: Moya.Method {
        switch self {
        case .category, .showCateNewsList, .submitComment:
            return .get
            
            
        }
    }
    
    var sampleData: Data {
        switch self {
        case .category:
            return "category test data".utf8Encoded
        case .showCateNewsList(let id):
            return "newslist id is \(id)".utf8Encoded

        case .submitComment(let postId, let name, let email, let content):
            return "\(postId),\(name),\(email),\(content)".utf8Encoded
        }
    }
    
    var task: Task {
        switch self {
        case .category: // Send no parameters
            return .requestPlain
            
        case .showCateNewsList(let id):
            return .requestParameters(parameters: ["id" : id], encoding: URLEncoding.queryString)
            
        case .submitComment(let postId, let name, let email, let content):
            return .requestParameters(parameters: ["post_id" : postId, "name" : name, "email" : email, "content" : content], encoding: URLEncoding.queryString)
            

    
            
            //        case .showCateNewsList(let id):
            //            var params: [String: Any] = [:]
            //            params["id"] = id
            //            return .requestParameters(parameters: params, encoding: URLEncoding.default)
            //        default:
            //            return .requestPlain
        }
    
        }
    var headers: [String: String]? {
        return ["Content-type": "application/json"]
    }


    
}

// MARK: - Helpers
private extension String {
    var urlEscaped: String {
        return addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
    }
    
    var utf8Encoded: Data {
        return data(using: .utf8)!
    }
}
